import { reactive, onMounted, ref, nextTick } from 'vue'
import useInstance from '@/hooks/useInstance';
import { rolePageApi } from '@/api/role/role'
import { RolePageParams, RoleTable } from '@/api/role/roleModel'
import { EditType } from '@/type/BaseEnum'
import { departmentPageApi } from '@/api/dept/dept'
export default function RoleTableDate() {
  const { global, proxy } = useInstance()
  var searchParams = reactive<RolePageParams>({
    name: '',
    deptId: '',
    page: 1,
    size: 10,
    sort: '',
  })
    var searchParamsDept = reactive({
    name: '',
    size: 100,
    page: 1,
    sort: ''
  })
  const roleTableDate = reactive<RoleTable>({
    list: [],
    Loading: false,
    height: 0,
    total: 0,
    DeptList:[]
  })
  onMounted(() => {
    getRoleList()
    getDeptList()
    nextTick(() => {
      roleTableDate.height = window.innerHeight - 185
    })
  })
  const getDeptList=async()=>{
    const res = await departmentPageApi(searchParamsDept)
    if(res.code ===200){
      roleTableDate.DeptList=res.data.records
    }
  }
  const getRoleList = async () => {
    roleTableDate.Loading = true
    const res = await rolePageApi(searchParams)
    if (res.code === 200) {
      roleTableDate.Loading = false
      roleTableDate.list = res.data.records
      roleTableDate.total = res.data.total
    }
  }
    const handleCurrentChange = (current) => {
    searchParams.page = current
    getRoleList()
  }
  const tableRowClassName = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 1) {
      return "warning-row";
    } else {
      return "success-row";
    }
  }
  const handleSizeChange = (size) => {
    searchParams.size = size
    getRoleList()
  }
  const resetBtn=()=>{
    searchParams.deptId=''
    searchParams.name=''
    searchParams.page=1
    searchParams.size=10
    searchParams.sort=''
    getRoleList()
  }
  const serachBtn=()=>{
    searchParams.page=1
    getRoleList()
  }
  return{
    searchParams,getRoleList,roleTableDate,handleCurrentChange,handleSizeChange,resetBtn,serachBtn,tableRowClassName
  }
}